import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login',
        component: Login
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/admin',
        component: () => import('../views/admin/Admin'),
        children: [
            {
                path: "index",
                component: () => import('../views/admin/Index'),
            },
            {
                path: 'self',
                name: 'AdminSelf',
                component: () => import('../views/self'),
            },
        ]
    },
    {
        path: '/admin/invigilate',
        name: 'Invigilate',
        component: () => import('../views/admin/examination/invigilate'),
    },

    {
        path: '/examination',
        name: 'Examination',
        component: () => import('../views/admin/Admin'),
        children: [
            {
                path: 'exam',
                name: 'Exam',
                component: () => import('../views/admin/examination/exam'),
            },
            {
                path: 'testPaper',
                name: 'TestPaper',
                component: () => import('../views/admin/examination/exam/testPaper'),
            },
            {
                path: 'questionBank',
                name: 'QuestionBank',
                component: () => import('../views/admin/examination/questionBank'),
            },
            {
                path: 'markingExamPapers',
                name: 'MarkingExamPapers',
                component: () => import('../views/admin/examination/exam/markingExamPapers'),
            },
        ]
    },

    {
        path: '/system',
        name: 'System',
        component: () => import('../views/admin/Admin'),
        children: [
            {
                path: 'user',
                name: 'User',
                component: () => import('../views/admin/sys/user'),
            },
            {
                path: 'role',
                name: 'Role',
                component: () => import('../views/admin/sys/role'),
            },
            {
                path: 'permission',
                name: 'Permission',
                component: () => import('../views/admin/sys/permission'),
            },
            {
                path: 'dict',
                name: 'Dict',
                component: () => import('../views/admin/sys/dict'),
            }, {
                path: 'systemReset',
                name: 'SystemReset',
                component: () => import('../views/admin/sys/systemReset'),
            },
        ]
    },
    {
        path: '/monitor',
        name: 'Monitor',
        redirect: "/monitor/dataMonitor",
        component: () => import('../views/admin/Admin'),
        children: [
            {
                path: 'dataMonitor',
                name: 'DataMonitor',
                component: () => import('../views/admin/monitor/DataMonitor'),
            },
        ]
    },
    {
        path: '/ei/answerSheet',
        name: 'AnswerSheet',
        component: () => import('../views/ei/answerSheet'),
    },
    {
        path: '/ei',
        component: () => import('../views/ei/Ei'),
        children: [
            {
                path: '',
                component: () => import('../views/ei/index'),
            },
            {
                path: 'myExam',
                name: 'MyExam',
                component: () => import('../views/ei/myExam'),
            },
            {
                path: 'admissionTicket',
                name: 'AdmissionTicket',
                component: () => import('../views/ei/myExam/admissionTicket'),
            },
            {
                path: 'myTestPaper',
                name: 'MyTestPaper',
                component: () => import('../views/ei/myExam/myTestPaper'),
            },
            {
                path: 'signup',
                name: 'Signup',
                component: () => import('../views/ei/exam/signup'),
            },
            {
                path: 'registrationForm',
                name: 'RegistrationForm',
                component: () => import('../views/ei/exam/registrationForm'),
            },
            {
                path: 'self',
                name: 'EiSelf',
                component: () => import('../views/self'),
            },
        ]
    },


    {
        path: '/404',
        name: '404',
        component: () => import('../views/404')
    },
    {
        path: '/:pathMatch(.*)',
        redirect: '/404'
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


router.beforeEach((to, from, next) => {
    let toPath = to.path
    let role = sessionStorage.getItem("role")
    if (
        toPath.startsWith("/admin") ||
        toPath.startsWith("/system") ||
        toPath.startsWith("/examination") ||
        toPath.startsWith("/monitor")
    ) {
        if (role === "admin") {
            next();
        } else {
            next("/login")
        }
    } else if (
        toPath.startsWith("/ei")
    ) {
        if (role === "ei") {
            next();
        } else {
            next("/login")
        }
    } else {
        next()
    }
})
export default router
